﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.Threading;

namespace Advertise
{
    public class Repost163 : IRepost
    {
                private System.Windows.Forms.WebBrowser webBrowser1 = null;
        private LearnNet.Common.XmlHelper xmlHelper = null;
        private static string code = "163";
        public Repost163(System.Windows.Forms.WebBrowser webBrowser1, LearnNet.Common.XmlHelper xmlHelper)
        {
            this.webBrowser1 = webBrowser1;
            this.xmlHelper = xmlHelper;
        }

        public string  Register(RegistEntity regEntity)
        {
            string strReturn = string.Empty;
            if (!ReportHelper.SetAttribute("root/station[@code='"+code+"']/register/name", regEntity.Name, webBrowser1, xmlHelper))
            {
                strReturn+="名称设置出错!";
            }
            if (!ReportHelper.SetAttribute("root/station[@code='"+code+"']/register/pwd", regEntity.Pwd,webBrowser1,xmlHelper))
            {
                strReturn+="密码设置出错!";
            }
            if (!ReportHelper.SetAttribute("root/station[@code='"+code+"']/register/surepwd", regEntity.SurePwd,webBrowser1,xmlHelper))
            {
                strReturn+="确认密码设置出错!";
            }
            if (!ReportHelper.SetAttribute("root/station[@code='"+code+"']/register/email", regEntity.Email,webBrowser1,xmlHelper))
            {
                strReturn+="电子邮件设置出错!";
            }
            if (!ReportHelper.SetAttribute("root/station[@code='"+code+"']/register/year", regEntity.Year,webBrowser1,xmlHelper))
            {
                strReturn+="年设置出错!";
            }
            if (!ReportHelper.SetAttribute("root/station[@code='"+code+"']/register/qq", regEntity.QQ,webBrowser1,xmlHelper))
            {
                strReturn+="QQ设置出错!";
            }
            if (!ReportHelper.SetAttribute("root/station[@code='" + code + "']/register/month", regEntity.Month, webBrowser1, xmlHelper))
            {
                strReturn+="月设置出错!";
            }
            if (!ReportHelper.SetAttribute("root/station[@code='"+code+"']/register/day", regEntity.Day,webBrowser1,xmlHelper))
            {
                strReturn+="日设置出错!";
            } 
            if (!ReportHelper.SetAttribute("root/station[@code='"+code+"']/register/question", regEntity.Question,webBrowser1,xmlHelper))
            {
                strReturn+="问题设置出错!";
            }
            if (!ReportHelper.SetAttribute("root/station[@code='"+code+"']/register/answer", regEntity.Answer,webBrowser1,xmlHelper))
            {
                strReturn+="答案设置出错!";
            }
            if (!ReportHelper.SetAttribute("root/station[@code='"+code+"']/register/verificationCode", regEntity.VerificationCode,webBrowser1,xmlHelper))
            {
                strReturn += "验证码设置出错!";
            }
            try
            {
                object obj = webBrowser1.Document.Forms["fReg0"].InvokeMember("submit");
            }
            catch (Exception e)
            {
                strReturn += e.Message;
            }
            return strReturn;
        }

        public string LoginIn(System.Windows.Forms.WebBrowser webBrowser1, RegistEntity regEntity)
        {
             string strReturn = string.Empty;
            if (!ReportHelper.SetAttribute("root/station[@code='" + code + "']/login/name", regEntity.Name, webBrowser1, xmlHelper))
            {
                strReturn += "名称设置出错!";
            }
            if (!ReportHelper.SetAttribute("root/station[@code='" + code + "']/login/pwd", regEntity.Pwd, webBrowser1, xmlHelper))
            {
                strReturn += "密码设置出错!";
            }

            try
            {
                object obj = null;

                 obj = webBrowser1.Document.GetElementById("loginBtn").InvokeMember("click");
              

            }
            catch (Exception e)
            {
                strReturn += e.Message;
            }
            return strReturn;
        }

        public string Report(System.Windows.Forms.WebBrowser webBrowser1, ReportContent reportContent)
        {

                    

            string strReturn = string.Empty;
            if (!ReportHelper.SetAttribute("root/station[@code='" + code + "']/report/title", reportContent.Title, webBrowser1, xmlHelper))
            {
                strReturn += "名称设置出错!";
            }
            if (!ReportHelper.SetAttribute("root/station[@code='" + code + "']/report/content", reportContent.Content, webBrowser1, xmlHelper))
            {
                strReturn += "内容设置出错!";
            }
          
            try
            {

                webBrowser1.Document.Window.Frames[1].Document.Body.InnerHtml = reportContent.Content;
                

            }
            catch (Exception ex)
            {
                strReturn += ex.Message;
            }
            try
            {
                Thread.Sleep(1000);
                object obj = null;
                HtmlElementCollection htmlCollection = webBrowser1.Document.GetElementsByTagName("input");
                for (int i = 0; i < htmlCollection.Count; i++)
                {
                    HtmlElement element = htmlCollection[i];

                    if (element.Name == "key-093402170-a")
                    {
                        element.InvokeMember("click");
                        break;
                    }

                }
                 //obj = webBrowser1.Document.GetElementById("articlePostBtn").InvokeMember("click");
                // obj = webBrowser1.Document.Forms["editorForm"].InvokeMember("submit");

            }
            catch (Exception e)
            {
                strReturn += e.Message;
            }

            return strReturn;
        }
    }
}
